//
//  ByUserMySetVC.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/14.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByUserMySetVC.h"
#import "ByLoginAndRegisterVC.h"

@interface ByUserMySetVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSArray *titles;
@end

@implementation ByUserMySetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self addLeftBackBarButtonItem];
    [self createViews];
}

#pragma mark - getter
- (NSArray *)titles{
    if (!_titles) {
        _titles = @[@[@"修改登录密码"],@[@"当前版本",@"关于我们",@"清除缓存"]];
    }
    return _titles;
}

#pragma mark - create views
- (void)createViews{
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-40);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor colorWithWhite:0.90 alpha:1];
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    
    UIButton *logoOutButton = [[UIButton alloc] init];
    [self.view addSubview:logoOutButton];
    [logoOutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 40));
        if (@available(iOS 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.bottom.equalTo(self.view.mas_bottom);
        }
    }];
    [logoOutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [logoOutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoOutButton setBackgroundColor:BY_THEME_COLOR];
    logoOutButton.titleLabel.font = FONT(16);
    [logoOutButton addTarget:self action:@selector(logoOut) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titles.count;;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sectionAry = self.titles[section];
    return sectionAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifer = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        cell.textLabel.font = FONT(14);
        cell.selectionStyle= UITableViewCellSeparatorStyleNone;
    }
    if (!(indexPath.section == 1 && (indexPath.row == 0 || indexPath.row == 2))) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSArray *sectionAry = self.titles[indexPath.section];
    cell.textLabel.text = sectionAry[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}

#pragma mark - 退出登录
- (void)logoOut{
    [ByCacheUser setUserInfo:nil];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:[[ByLoginAndRegisterVC alloc] init]];
    window.rootViewController = navi;
}
@end
